package com.xcjh.smartchat.vm

import androidx.lifecycle.viewModelScope
import com.xcjh.base_lib.base.BaseViewModel
import com.xcjh.base_lib.utils.request
import com.xcjh.base_lib.utils.requestStream
import com.xcjh.smartchat.net.apiService
import kotlinx.coroutines.launch
import okio.BufferedSource
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader

class MainVm : BaseViewModel() {


    fun getData() {
        request(
            { apiService.getCountry() },
            {

            }, {
                //请求失败

            }, false, "loading..."
        )
    }

    fun getStreamTest() {
        viewModelScope.launch {
            runCatching {
                //请求体
                apiService.streamTest()
            }.onSuccess {
                //网络请求成功 关闭弹窗
                loadingChange.dismissDialog.postValue(false)
                runCatching {
                    //校验请求结果码是否正确，不正确会抛出异常走下面的onFailure
                    val source = it.source()


                    ///val reader = it.byteStream().buffered(3)
                    ///1
                    it.byteStream().bufferedReader().use { reader ->
                        val results = StringBuilder()
                        while (true) {
                            val line = reader.readLine() ?: break
                            results.append(line)
                        }
                        val inputAsString = results.toString()
                    }
                    ///2
                    val inputString = it.byteStream().bufferedReader().useLines { lines ->
                        val results = StringBuilder()
                        lines.forEach { results.append(it) }
                        results.toString()
                    }

                }.onFailure { e ->
                    e.printStackTrace()
                    //失败回调
                }
            }.onFailure {
                //网络请求异常 关闭弹窗
                loadingChange.dismissDialog.postValue(false)
                //打印错误栈信息
                it.printStackTrace()
                //失败回调
            }
        }
        requestStream(
            { apiService.streamTest() },
            {
                while (true){
                   val line= it.readUtf8Line() ?: break
                }
                it.close()
               // streamFromBufferedSource(it)
            }, {
                //请求失败

            }, false, "loading..."
        )
    }

/*    private Observable<String> streamFromBufferedSource(final BufferedSource source) {
        return Observable.create(new Action1<Emitter<String>>() {
            @Override
            public void call(Emitter<String> stringEmitter) {
                try {
                    while (!source.exhausted()) {
                        stringEmitter.onNext(source.readUtf8Line());
                    }
                    stringEmitter.onCompleted();
                } catch (IOException e) {
                    stringEmitter.onError(e);
                }
            }
        }, BackpressureMode.BUFFER);
    }*/
}
